package com.rpc.netty.httpNetty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;


/**
 * @author 何章怀晓 hezhanghuaixiao
 * @version V1.0
 * @desc
 */
public class NettyServer {

  public static void main(String[] args) {

    EventLoopGroup bossGroup = new NioEventLoopGroup();

    EventLoopGroup workerGroup = new NioEventLoopGroup();

    try {

      ServerBootstrap bootstrap = new ServerBootstrap();
      //引导程序
      bootstrap.group(bossGroup,workerGroup)
          .channel(NioServerSocketChannel.class)
          .childHandler(new HttpServerInit());


      System.out.println("服务器准备好了...");
      //绑定端口，并同步，生成cf对象
      //并启动服务器
      ChannelFuture cf = bootstrap.bind(8000).sync();

      cf.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
          if(future.isDone()){
            System.out.println("操作完成");
            if(future.isSuccess()){
              System.out.println("操作成功");
            }else {
              System.out.println("操作失败");
            }
          }
        }
      });



      cf.channel().closeFuture().sync();

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      bossGroup.shutdownGracefully();
      workerGroup.shutdownGracefully();



    }


  }

}
